const api = require('../../../api/mop_api');
import { toast, log, err, to } from '../../../utils/wxapi';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodlist: [],
    page: 1,
    size: 10,
    pageTotal: 0,
    cha: '',
  },
  // 修改
  edit(e) {
    const { id } = e.currentTarget.dataset.item;
    to('/dj/pages/addgood/addgood?id=' + id);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.goodlist();
  },
  async goodlist() {
    try {
      const { page, size, goodlist } = this.data;
      const res = await api.goodList({ page, size });
      const { respCode, source, pageTotal, msg } = res;
      if (respCode === 200) {
        let newGoodlist = JSON.parse(source);
        newGoodlist = this.arrjson(newGoodlist);
        if (goodlist.length === 0) {
          this.setData({ goodlist: newGoodlist, pageTotal });
        } else {
          this.setData({ goodlist: goodlist.concat(newGoodlist), pageTotal });
        }
      } else {
        toast(msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  arrjson(arr) {
    try {
      arr.forEach(e => {
        e.coverUrl = JSON.parse(e.coverUrl);
      });
      return arr;
    } catch (error) {
      err(error);
      throw error;
    }
  },
  delgood(e) {
    try {
      const { id } = e.currentTarget.dataset;
      wx.showModal({
        title: '提示',
        content: '你确定删除该商品吗？',
        complete: async res => {
          if (res.confirm) {
            const res = await api.goodDel({ id });
            if (res.respCode === 200) {
              this.setData({
                goodlist: [],
                page: 1,
              });
              this.goodlist();
              toast('删除成功');
            } else {
              toast(res.msg);
            }
          }
        },
      });
    } catch (error) {
      err(error);
      throw error;
    }
  },
  add() {
    to('/dj/pages/addgood/addgood');
  },
  async putsta(e) {
    try {
      const { id, status } = e.currentTarget.dataset.item;
      const newStatus = status === 'DISABLED' ? 'NORMAL' : 'DISABLED';
      const data = { id, status: newStatus };
      const res = await api.goodsta(data);
      log(res);
      if (res.respCode === 200) {
        this.setData({
          page: 1,
          goodlist: [],
        });
        this.goodlist();
        toast('修改成功');
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  async query() {
    try {
      const title = this.data.cha;
      const res = await api.goodList({ title });
      if (res.respCode === 200) {
        let goodlist = JSON.parse(res.source);
        goodlist = this.arrjson(goodlist);
        this.setData({ goodlist });
      } else {
        toast(res.msg);
      }
    } catch (error) {
      err(error);
      throw error;
    }
  },
  onHide() {
    this.setData({
      goodlist: [],
      page: 1,
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { goodlist, pageTotal, page } = this.data;
    if (goodlist.length < pageTotal) {
      this.setData({ page: page + 1 });
      this.goodlist();
    } else {
      toast('已加载全部数据');
    }
  },
});
